package org.cskt.sl.member.controller;

import java.io.File;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;

import javax.servlet.http.HttpSession;

import org.cskt.sl.member.entity.Users;
import org.cskt.sl.member.entity.UsersExample;
import org.cskt.sl.member.service.UsersService;
import org.cskt.sl.util.Pager;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.ModelAndView;

@Controller
/*
 * uc2
 */
public class UsersController {
	
	@Autowired
	private UsersService userService;
	
	private Integer pageSize=3;
	
	@RequestMapping("/login.do")
	@ResponseBody
	public Map<String, String> user_login(Users user,HttpSession session){
		
		ModelAndView mv=new ModelAndView();
		mv.setViewName("error");
		
		Map<String, String> map=new HashMap<>();
		
		user = userService.login(user);
		
		if(user!=null){
			session.setAttribute("loginUser", user);
			map.put("flag", "true");
		}else{
			map.put("flag", "false");
			map.put("errormsg", "用户名或密码错误!");
		}
		
		return map;
	}

	@RequestMapping("/user_list.do")
	public ModelAndView user_list(HttpSession session,Integer pageNo,String keyword){
		
		ModelAndView mv=new ModelAndView();
		mv.setViewName("error");
		
		UsersExample example=new UsersExample();
		
		if(keyword==null){
			keyword=(String) session.getAttribute("userkeyword");
			if(keyword==null){
				keyword="";
			}
		}else{
			session.setAttribute("userkeyword", keyword);
		}
		
		example.createCriteria().andRealnameLike("%"+keyword+"%");
		example.or(example.createCriteria().andUseridLike("%"+keyword+"%"));
		
		if(pageNo==null){
			pageNo=1;
		}
		
		Pager pager=new Pager(pageSize,userService.countByExample(example),pageNo);
		
		
		List<Users> ulist=userService.findUserPageList(example, pager);
		
		if(ulist!=null){
			mv.addObject("ulist", ulist);
			mv.addObject("p", pager);
			mv.setViewName("member/member_list");
		}
		
		return mv;
	}
	
	@RequestMapping("/user_list_id.do")
	public ModelAndView user_list_id(HttpSession session,Integer pageNo,String keyword,String userid){
		
		ModelAndView mv=new ModelAndView();
		mv.setViewName("error");
		
		UsersExample example=new UsersExample();
		Users loginUser=(Users) session.getAttribute("loginUser");
		
		if(loginUser!=null){
			userid=loginUser.getUserid();
		}else{
			mv.addObject("message", "您没有登录!!!");
			return mv;
		}
		
		if(keyword==null){
			keyword=(String) session.getAttribute("userkeyword");
			if(keyword==null){
				keyword="";
			}
		}else{
			session.setAttribute("userkeyword", keyword);
		}
		
		example.createCriteria().andRealnameLike("%"+keyword+"%").andReferrerEqualTo(userid);
		example.or(example.createCriteria().andUseridLike("%"+keyword+"%"));
		
		if(pageNo==null){
			pageNo=1;
		}
		
		Pager pager=new Pager(pageSize,userService.countByExample(example),pageNo);
		
		
		List<Users> ulist=userService.findUserPageList(example, pager);
		
		if(ulist!=null){
			mv.addObject("ulist", ulist);
			mv.addObject("p", pager);
			mv.setViewName("member/member_list");
		}
		
		return mv;
	}
	
	@RequestMapping("/regist_check.do")
	public ModelAndView user_regist(Users user,HttpSession session,MultipartFile cardf,MultipartFile cardv,MultipartFile bankimg) throws Exception{
		
		ModelAndView mv=new ModelAndView();
		mv.setViewName("error");
		
		if(user!=null){
			
			String cardfpath= saveFileToPic(cardf,user,session);
			user.setCardfront(cardfpath);

			String cardvpath= saveFileToPic(cardv,user,session);
			user.setCardverso(cardvpath);

			String bankpath= saveFileToPic(bankimg,user,session);
			user.setBankimage(bankpath);
			
			session.setAttribute("registUser", user);
			mv.setViewName("member/regist_comfirm");
			mv.addObject("user", user);
		}
		
		return mv;
	}
	
	private String saveFileToPic(MultipartFile cardf,Users user,HttpSession session) throws Exception {
		String suffix=cardf.getOriginalFilename().substring(cardf.getOriginalFilename().lastIndexOf('.'));
		String path=UUID.randomUUID().toString()+suffix;
		File dir=new File(session.getServletContext().getRealPath("/upload"));
		if(!dir.exists()){
			dir.mkdir();
		}
		System.out.println(path);
		System.out.println(dir);
		File file =new File(dir, path);
		cardf.transferTo(file);
		return path;
	}

	@RequestMapping("/regist_confirm.do")
	public ModelAndView regist_confirm(HttpSession session){
		
		ModelAndView mv=new ModelAndView();
		mv.setViewName("error");
		
		Users user=(Users) session.getAttribute("registUser");
		if(user!=null){
			defaultUser(user);
			userService.addUser(user);
			mv.setViewName("member/regist_success");
			mv.addObject("user", user);
			session.removeAttribute("registUser");
		}
		return mv;
	}
	
//    36073019970528113x 
	private void defaultUser(Users user) {
		user.setPartid(1);
		user.setMembertype(1);
		user.setState(0);
		user.setPassword(user.getCardid().substring(12));
		user.setHighpass(user.getCardid().substring(12));
	}

	@ResponseBody
	@RequestMapping("/modify_pwd.do")
	public Map<String, String> user_modify_pwd(HttpSession session,String oldPwd,String newPwd,String rePwd){
		
		Map<String, String> map=new HashMap<>();
		map.put("status", "error");
		
		if(oldPwd==null||newPwd==null||rePwd==null||"".equals(oldPwd)||"".equals(newPwd)||"".equals(rePwd)){
			map.put("status", "null");
		}else{
			if(rePwd.equals(newPwd)){
				Users loginUser=(Users) session.getAttribute("loginUser");
				if(loginUser==null){
					map.put("status", "nologin");
					return map;
				}
				if(loginUser.getPassword().equals(oldPwd)){
					userService.updatePwdById(loginUser.getUid(), newPwd);
					session.invalidate();
					map.put("status", "ok");
				}else{
					map.put("status", "pwd");
				}
			}else{
				map.put("status", "repwd");
			}
		}
		return map;
	}

	@ResponseBody
	@RequestMapping("/modify_highpwd.do")
	public Map<String, String> user_modify_highpwd(HttpSession session,String oldPwd,String newPwd,String rePwd){
		
		Map<String, String> map=new HashMap<>();
		map.put("status", "error");
		
		if(oldPwd==null||newPwd==null||rePwd==null||"".equals(oldPwd)||"".equals(newPwd)||"".equals(rePwd)){
			map.put("status", "null");
		}else{
			if(rePwd.equals(newPwd)){
				Users loginUser=(Users) session.getAttribute("loginUser");
				if(loginUser==null){
					map.put("status", "nologin");
					return map;
				}
				if(loginUser.getHighpass().equals(oldPwd)){
					userService.updateSecondPwdById(loginUser.getUid(), newPwd);
					session.invalidate();
					map.put("status", "ok");
				}else{
					map.put("status", "pwd");
				}
			}else{
				map.put("status", "repwd");
			}
		}
		return map;
	}
	
	@RequestMapping("/go_pwd.do")
	public ModelAndView go_Pwd(){
		
		ModelAndView mv=new ModelAndView();
		mv.setViewName("member/user_Pwd");
		
		return mv;
	}

	@RequestMapping("/go_update.do")
	public ModelAndView go_update(HttpSession session){
		
		ModelAndView mv=new ModelAndView();
		mv.setViewName("error");
		
		Users user=(Users) session.getAttribute("loginUser");
		
		if(user!=null){
			mv.addObject("user",user);
			mv.setViewName("member/update");
		}else{
			mv.addObject("message","尼玛炸了,没登陆哦! 点击<a href='login.jsp'>登录!</a>");
		}
		
		return mv;
	}
	

	@ResponseBody
	@RequestMapping("/user_update_ajax.do")
	public Map<String, Object> go_update_ajax( Users user){
		
		Map<String, Object> map=new HashMap<>();
		map.put("status", "error");
		
		System.out.println("AJAXREQUEST:"+user);
		if(user!=null){
			int rs=userService.updateUserById(user);
			if(rs>0){
				map.put("status", "ok");
			}
		}else{
			map.put("status", "null");
		}
		
		return map;
	}
	
	@RequestMapping("/user_del_ajax.do")
	public ModelAndView user_del_ajax(Integer id){
		
		ModelAndView mv=new ModelAndView();
		mv.setViewName("error");
		
		if(id!=null){
			int rs=userService.deleteById(id);
			if(rs>0){
				mv.setViewName("redirect:user_list_id.do");
			}
		}else{
			mv.addObject("message", "妈的空值狗滚!");
		}
		
		return mv;
	}
	
	@RequestMapping("/user_update.do")
	public ModelAndView user_update(HttpSession session,Users user){
		
		ModelAndView mv=new ModelAndView();
		mv.setViewName("error");
		
		//System.out.println(user);
		if(user!=null){
			int rs=userService.updateUserById(user);
			if(rs>0){
				session.setAttribute("loginUser", user);
				mv.setViewName("member/update");
				mv.addObject("user", user);
			}
		}
		return mv;
	}
	
	@ResponseBody
	@RequestMapping("/user_get.do")
	public Map<String, Object> user_get(HttpSession session,Integer id){
		
		Map<String, Object> map=new HashMap<>();
		map.put("status", "errsor");
		
		if(id!=null){
			Users user=userService.getUserById(id);
			if(user!=null){
				System.out.println(user);
				map.put("status", "ok");
				map.put("data", user);
			}else{
				map.put("status", "null");
			}
		}
		return map;
	}
}
